<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="120px" @keyup.enter.native="handleQuery">
                <el-form-item label="handle" prop="expert_name">
                    <el-input v-model.trim="queryParams.expert_name" placeholder="请输入" clearable size="small" style="width: 240px" />
                </el-form-item>
                <el-form-item label="投手" prop="fb_admin_id">
                    <SelectPage
                        v-model="queryParams.fb_admin_id"
                        placeholder="请选择"
                        :url="$api.purchase.getPurchaserList"
                        show_field="username"
                        show_id="operater_id"
                        query_field="keyword"
                        filterable
                        clearable
                        popper-class="custom-select"
                        style="width: 240px"
                    ></SelectPage>
                </el-form-item>
                <el-form-item label="广告名称" prop="adv_name">
                    <el-input v-model="queryParams.adv_name" placeholder="请输入" clearable size="small" style="width: 240px" />
                </el-form-item>
                <el-form-item label="投放状态" prop="publish_status">
                    <el-select v-model="queryParams.publish_status" placeholder="请选择" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in PUBLISH_STATUS" :key="index" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="SKU" prop="sku">
                    <el-input v-model="queryParams.sku" placeholder="请输入" clearable size="small" style="width: 240px" />
                </el-form-item>

                <el-form-item label="开始时间" prop="start_time">
                    <el-date-picker
                        v-model="queryParams.start_time"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间"
                        style="width: 240px"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="结束时间" prop="end_time">
                    <el-date-picker
                        v-model="queryParams.end_time"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间"
                        style="width: 240px"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="创建时间" prop="create_time">
                    <el-date-picker
                        v-model="queryParams.create_time"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间"
                        style="width: 240px"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" @click="showDetailDialog({})">新增</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" :columns="columns" @queryTable="getList"></right-toolbar>
            </el-row>
            <!-- <el-tabs v-model="queryParams.status" type="border-card" @tab-click="handleQuery">
                <el-tab-pane v-for="item in tabsList" :key="item.value" :label="item.label" :name="item.value.toString()"></el-tab-pane>
            </el-tabs>
            <div class="table-wrap"> -->
            <el-table v-loading="loading" :data="tableData" border script>
                <el-table-column v-if="showColumn('expert_id')" label="达人ID" prop="expert_id" min-width="140"></el-table-column>
                <el-table-column v-if="showColumn('expert_name')" label="handle" prop="expert_name" min-width="160">
                    <template slot-scope="scope">
                        <el-link type="primary" @click="gotoPersonalDetail(scope.row)">{{ scope.row.expert_name }}</el-link>
                    </template>
                </el-table-column>
                <el-table-column v-if="showColumn('publish_url')" label="视频链接" prop="publish_url" min-width="220">
                    <template slot-scope="scope">
                        <el-tooltip :content="scope.row.publish_url" placement="top">
                            <el-link v-if="scope.row.publish_url" type="primary" :href="scope.row.publish_url" target="_blank" class="ellipsis-2">{{ scope.row.publish_url }}</el-link>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column v-if="showColumn('adv_name')" label="广告名称" prop="adv_name" min-width="220">
                    <template slot-scope="scope">
                        <el-tooltip :content="scope.row.adv_name" placement="top">
                            <span class="ellipsis-2">{{ scope.row.adv_name }}</span>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column v-if="showColumn('ads_name')" label="广告系列" prop="ads_name" min-width="120">
                    <template slot-scope="scope">
                        <el-tooltip :content="scope.row.ads_name" placement="top">
                            <span class="ellipsis-2">{{ scope.row.ads_name }}</span>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column v-if="showColumn('sku')" label="SKU" prop="sku" min-width="160"></el-table-column>
                <el-table-column v-if="showColumn('spend')" label="花费" prop="spend" min-width="120"></el-table-column>
                <el-table-column v-if="showColumn('order_nums')" label="订单数" prop="order_nums" min-width="120"></el-table-column>
                <el-table-column v-if="showColumn('roi')" label="ROI" prop="roi" min-width="120"></el-table-column>
                <el-table-column v-if="showColumn('cpm')" label="CPM" prop="cpm" min-width="120"></el-table-column>
                <el-table-column v-if="showColumn('cpc')" label="CPC" prop="cpc" min-width="120"></el-table-column>
                <el-table-column v-if="showColumn('ctr')" label="CTR" prop="ctr" min-width="120"></el-table-column>
                <el-table-column v-if="showColumn('publish_status')" label="投放状态" prop="publish_status" min-width="100">
                    <template slot-scope="scope">
                        {{ scope.row.publish_status | formatLabelByValueFuzzy(PUBLISH_STATUS) }}
                    </template>
                </el-table-column>
                <el-table-column v-if="showColumn('operater')" label="投手" prop="operater" min-width="120"></el-table-column>
                <el-table-column v-if="showColumn('start_time')" label="开始日期" prop="start_time" min-width="160"></el-table-column>
                <el-table-column v-if="showColumn('end_time')" label="结束时间" prop="end_time" min-width="160"></el-table-column>
                <el-table-column v-if="showColumn('adv_platform_id')" label="平台" prop="adv_platform_id" min-width="160">
                    <template slot-scope="scope">
                        {{ scope.row.adv_platform_id | formatLabelByValueFuzzy(PUBLISH_PLATFORM) }}
                    </template>
                </el-table-column>
                <el-table-column v-if="showColumn('create_time')" label="创建时间" prop="create_time" min-width="160"></el-table-column>
                <el-table-column label="操作" min-width="120" fixed="right">
                    <template slot-scope="scope">
                        <div>
                            <el-button type="text" icon="el-icon-edit" style="padding: 4px 0" @click="showDetailDialog(scope.row, 'edit')">编辑</el-button>
                        </div>
                        <div>
                            <el-button type="text" icon="el-icon-delete" style="padding: 4px 0" @click="deleteRow(scope.row)">删除</el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <!-- </div> -->
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
        </div>
        <detail-dialog v-model="detailDialog.visible" v-if="detailDialog.visible" :params="detailDialog.data" @success="handleQuery"></detail-dialog>
    </div>
</template>

<script>
import { getAdvList, delAdv } from '@/api/expert'
import { PUBLISH_STATUS, PUBLISH_PLATFORM } from '../constant'
import DetailDialog from './components/DetailDialog.vue'
export default {
    name: 'ExpertAdvertisementList',
    components: { DetailDialog },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            PUBLISH_STATUS,
            PUBLISH_PLATFORM,
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                expert_name: undefined,
                fb_admin_id: undefined,
                adv_name: undefined,
                publish_status: undefined,
                sku: undefined,
                start_time: [],
                end_time: [],
                create_time: [],
            },
            detailDialog: {
                visible: false,
                data: {},
            },
            columns: [
                { label: '达人ID', prop: 'expert_id', visible: true },
                { label: 'handle', prop: 'expert_name', visible: true },
                { label: '视频链接', prop: 'publish_url', visible: true },
                { label: '广告名称', prop: 'adv_name', visible: true },
                { label: '广告系列', prop: 'ads_name', visible: true },
                { label: 'SKU', prop: 'sku', visible: true },
                { label: '花费', prop: 'spend', visible: true },
                { label: '订单数', prop: 'order_nums', visible: true },
                { label: 'ROI', prop: 'roi', visible: true },
                { label: 'CPM', prop: 'cpm', visible: true },
                { label: 'CPC', prop: 'cpc', visible: true },
                { label: 'CTR', prop: 'ctr	', visible: true },
                { label: '投放状态', prop: 'publish_status', visible: true },
                { label: '投手', prop: 'operater', visible: true },
                { label: '开始日期', prop: 'start_time', visible: true },
                { label: '结束时间', prop: 'end_time', visible: true },
                { label: '平台', prop: 'adv_platform_id', visible: true },
                { label: '创建时间', prop: 'create_time', visible: true },
            ],
        }
    },
    created() {
        this.getList()
    },
    methods: {
        gotoPersonalDetail({expert_id}) {
            this.$router.push({
                path: '/kol/expertView',
                query: {
                    expert_id,
                },
            })
        },
        showColumn(prop) {
            const data = this.columns.find(c => c.prop === prop)
            return data?.visible ?? true
        },
        deleteRow(row) {
            this.$confirm('是否确认删除该数据?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    delAdv({ id: row.id }).then(res => {
                        this.$message.success('删除成功')
                        this.handleQuery()
                    })
                })
                .catch(() => {})
        },
        showDetailDialog(row) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
        },
        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            getAdvList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>
<style>
.ellipsis-2 .el-link--inner {
    width: 100%;
    overflow: hidden;
    word-wrap: break-word;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
}
</style>
<style scoped>
.ellipsis-2 {
    width: 100%;
    overflow: hidden;
    word-wrap: break-word;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
}
::v-deep .el-tabs .el-tabs__content {
    padding: 0;
    border-bottom: 0;
}
.el-tabs--border-card {
    box-shadow: none;
    border-bottom: 0;
}

::v-deep .el-tabs .el-tabs__content {
    padding: 0;
    border-bottom: 0;
}
.table-wrap {
    padding: 15px;
    border-bottom: 1px solid #dfe4ed;
    border-left: 1px solid #dfe4ed;
    border-right: 1px solid #dfe4ed;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);
}
</style>
